import dayjs, { Dayjs } from 'dayjs';
type DateType = string | number | Date | Dayjs | null | undefined;

// 格式化日期
export function formatDate(
  date: DateType,
  format: string = 'YYYY-MM-DD HH:mm:ss',
  fallbackValue: string = '-',
) {
  if (!date) return fallbackValue;
  const parsedDate = dayjs(date);
  if (!parsedDate.isValid()) return fallbackValue;
  return parsedDate.format(format);
}

// 获取时间戳
export function getTimestamp(date: DateType, fallbackValue: string = '-') {
  if (!date) return fallbackValue;
  const parsedDate = dayjs(date);
  if (!parsedDate.isValid()) return fallbackValue;
  return parsedDate.valueOf();
}
